<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="视频标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入视频标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否私密" prop="privacy">
        <el-select
          v-model="queryParams.privacy"
          placeholder="请选择是否私密"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value as number"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.VLOG_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="视频信息" align="center" prop="videoInfo" min-width="120px">
        <template #default="scope">
          <VideoPlayer
            :title="scope.row.title"
            :url="scope.row.playUrl"
            :poster="scope.row.coverUrl"
          />
        </template>
      </el-table-column>
      <el-table-column label="作者信息" align="center" prop="userInfo" min-width="100px">
        <template #default="scope">
          <el-avatar
            fit="cover"
            :src="scope.row.user?.avatar || '/avatar.png'"
            class="flex-none w-50px h-50px"
            @click="imagePreview(scope.row.user?.avatar)"
          />
          <br />
          <el-text>{{ scope.row.user?.nickname || '匿名' }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="统计信息" align="center" prop="userInfo" min-width="100px">
        <template #default="scope">
          <el-text>点赞数：{{ scope.row.likedCount }}</el-text>
          <br />
          <el-text>收藏数：{{ scope.row.favoriteCount }}</el-text>
          <br />
          <el-text>分享数：{{ scope.row.shareCount }}</el-text>
          <br />
          <el-text>评论数：{{ scope.row.commentCount }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="其他信息" align="center" prop="userInfo" min-width="100px">
        <template #default="scope">
          是否私密：<dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.privacy" />
          <br />
          状态：<dict-tag type="vlog_status" :value="scope.row.status" />
          <br />
          <el-text>创建时间：{{ formatDate(scope.row.createTime) }}</el-text>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['vlog:info:update']"
          >
            修改状态
          </el-button>
          <br />
          <el-button
            link
            type="primary"
            @click="openLikedList(scope.row.id)"
            v-hasPermi="['vlog:info:query']"
          >
            查看点赞用户
          </el-button>
          <br />
          <el-button
            link
            type="primary"
            @click="openFavoriteList(scope.row.id)"
            v-hasPermi="['vlog:info:query']"
          >
            查看收藏用户
          </el-button>
          <br />
          <el-button
            link
            type="primary"
            @click="openProductList(scope.row.id)"
            v-hasPermi="['vlog:info:query']"
          >
            查看关联产品
          </el-button>
          <br />
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['vlog:info:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：修改 -->
  <VLogInfoForm ref="formRef" @success="getList" />
  <!-- 点赞用户列表弹窗 -->
  <VLogLikedList ref="likedListRef" />
  <!-- 收藏用户列表弹窗 -->
  <VLogFavoriteList ref="favoriteListRef" />
  <!-- 关联产品列表弹窗 -->
  <VLogProductList ref="productListRef" />
</template>

<script setup lang="ts">
import { VLogInfoApi, VLogInfoVO } from '@/api/vlog/info'
import { createImageViewer } from '@/components/ImageViewer'
import { DICT_TYPE, getBoolDictOptions, getIntDictOptions } from '@/utils/dict'
import download from '@/utils/download'
import { formatDate } from '@/utils/formatTime'
import VLogFavoriteList from './components/VLogFavoriteList.vue'
import VLogInfoForm from './components/VLogInfoForm.vue'
import VLogLikedList from './components/VLogLikedList.vue'
import VLogProductList from './components/VLogProductList.vue'

/** 短视频 列表 */
defineOptions({ name: 'VLogInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<VLogInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  title: undefined,
  privacy: undefined,
  createTime: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await VLogInfoApi.getVLogInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 大图预览 */
const imagePreview = (imgUrl: string) => {
  createImageViewer({
    urlList: [imgUrl]
  })
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number | string) => {
  formRef.value.open(type, id)
}

/** 点赞的用户列表 */
const likedListRef = ref()
const openLikedList = (id: number | string) => {
  likedListRef.value.open(id)
}

/** 收藏的用户列表 */
const favoriteListRef = ref()
const openFavoriteList = (id: number | string) => {
  favoriteListRef.value.open(id)
}

/** 关联的产品列表 */
const productListRef = ref()
const openProductList = (id: number | string) => {
  productListRef.value.open(id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number | string) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await VLogInfoApi.deleteVLogInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await VLogInfoApi.exportVLogInfo(queryParams)
    download.excel(data, '短视频.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
